I'm gonna overwrite a lot of this notebook's old content. I changed the way I'm calculating wt, and wanna test that my training worked.


In [1]:
from pearce.emulator import OriginalRecipe, ExtraCrispy
from pearce.mocks import cat_dict
import numpy as np
from os import path

In [2]:
import matplotlib
#matplotlib.use('Agg')
from matplotlib import pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()

In [3]:
training_file = '/u/ki/swmclau2/des/xi_cosmo_trainer/PearceRedMagicXiCosmoFixedNd.hdf5'
test_file = '/u/ki/swmclau2/des/xi_cosmo_tester/PearceRedMagicXiCosmoFixedNd_test.hdf5'
em_method = 'gp'
split_method = 'random'

In [4]:
a = 1.0
z = 1.0/a - 1.0

In [5]:
fixed_params = {'z':z}#, 'r':24.06822623}

In [6]:
n_leaves, n_overlap = 5000, 1
emu = ExtraCrispy(training_file, n_leaves, n_overlap, split_method, method = em_method, fixed_params=fixed_params,
                 custom_mean_function = 'linear', downsample_factor = 1.0)


/u/ki/swmclau2/.local/lib/python2.7/site-packages/pearce/emulator/emu.py:264: UserWarning: WARNING: NaN detected. Skipped 0 points in training data.
  warnings.warn('WARNING: NaN detected. Skipped %d points in training data.' % (num_skipped))
emu = OriginalRecipe(training_file, method = em_method, fixed_params=fixed_params,\ custom_mean_function = 'linear', downsample_factor=0.7)#, #hyperparams = {'n_estimators': 500, # 'max_depth': 5})

In [7]:
emu.x.shape


Out[7]:
(5000, 144, 12)

In [8]:
emu.get_param_names()


Out[8]:
['ombh2',
 'omch2',
 'w0',
 'ns',
 'ln10As',
 'H0',
 'Neff',
 'logM0',
 'sigma_logM',
 'logM1',
 'alpha',
 'r']
names = ['amp'] names.extend(emu.get_param_names()) dict(zip(names, np.array([1.282969797894147446e+00, 8.951664721378283575e+05, 7.830465404301183298e+04, 5.388438442608223624e-05, 4.398317466650228198e+03, 3.169582088726115320e-04, 1.253707987440919602e-01, 9.682153059967076467e-06, 2.348234600554276300e-05, 1.009271514630569800e+04, 2.837303616516211350e-04, 1.208270935044775918e-05, 5.004939102700953768e-01])))
v = np.array([-2.76674688, 39.90074862, 41.97733577, 34.908274 , 25.45566919, 32.9653712 , 45.25126079, 44.36071403, 43.39705654, 31.30384076, 29.14249287, 33.35982685, 68.25178863, 55.50543659, 0.5289175 , 3.26280959, 4.88974786, 5.20163106, 3.57189386, 4.0222253 , 35.7317967 , 3.04556806, 2.37166337, 1.22982813, 3.15514574, 1.06641224, 31.83494205, 0.12732846, 2.85868658])
v = np.array([-3.49251486e+00, 2.56895286e+02, 6.34889263e+02, 4.23540423e+00, 3.06144854e+01, 5.32898437e+02, 4.65947388e+00, 2.10953081e-01, 3.69172960e+02, 1.85079107e+02, 3.14421111e+02, 3.45236442e+02, 2.62822319e+02, 4.87881663e+02, 1.61183292e+00, 8.53907861e+02, 9.39441074e+01, 2.41645851e+01, 8.46587668e+02, 3.01748847e+02, 1.81427178e+00, 1.72439999e+00, 4.14188992e+00, 3.80508709e+00, -1.63855507e+00, 3.42620859e+02, 3.57459354e+00])
v = np.array([-2.51875005e+00, 3.99882406e+02, 2.59229552e+01, 2.91540851e+01, 1.39254458e+02, 8.12407915e+01, 2.49876072e+02, 2.74786641e+02, 4.56201422e+03, 6.03402392e+03, 8.23854446e+03, 6.63104020e+03, 5.20905761e+03, 1.94554178e+02, -9.88314607e-01, -1.11352182e+00, 2.77140729e+01, 2.82694588e+01, 1.17727291e+02, 1.27938574e+02, -2.12175268e+00, -1.49398245e+00, -2.46909795e+00, -8.50671268e-01, -3.76457784e+00])
v = np.array([-107.63201595, -3.00623478, 1.30030219, 73.91837015, -1.24519356, 61.31984424, -2.73478191, 80.84539419, 86.27226179, -16.47626201, -30.44683545, -35.42641201, -12.96207967, -43.95549179, -3.36130433, 37.9852565 , 3.28282274, 57.71964132, 105.93552856, 30.52956235, 81.30883655, 46.50496736, -4.63570007, 2.25765005, -11.86949411, 83.41893473, 75.44653974])
v = np.array([ -3.70809017, -3.44052739, 13.41401445, 10.36063547, -9.56185908, 8.74668577, -7.97667636, -1.31782856, -9.98796727, -9.63452286, 7.68799877, -7.48368788, -11.23455619, 1.13359274, -3.41003473, 12.78588911, 9.81411492, -9.12662602, 8.91865132, -7.61937602, -1.7967975, -11.04892756, -9.49986017, 9.41407695, -8.69062493, -11.28011457, -1.44207183] )
#zhongxu kenrel param_names = ['Omegam', 'Omegab', 'sigma_8', 'h', 'n_s', 'Neff', 'w', 'M_sat', 'alpha', 'Mcut', 'sigma_logM', 'eta_con', 'eta_vc', 'eta_vs', 'gamma_f'] v = np.array([ 0.2661017, 0.1054246, 1.1295944, 0.3643993, 0.2408568, 11.5649985, 5.6407612, 4.9071932, 10.6279446, 11.7621938, 4.7031938, 6.3770235, 11.7578699, 11.7547548, 8.4866085,/ -12.0550382, 1.8339794, 10.6161248, 2.2441632, 13.8155106, 10.6371797, 11.3512804, 7.342365 , 3.1795786, 3.7658774, 5.0188608, 4.6846614, 13.8155106, 13.8155106, 5.545777 , 13.8155106,\ -1.5383083, -13.8155106])
zhongzhu_dict = {'omch2':[0.2661017,1.8339794 ], 'ombh2':[0.1054246,10.6161248], 'ln10As':[1.1295944,2.2441632],\ 'H0':[0.3643993,13.8155106],\ 'ns':[0.2408568,10.6371797], 'Neff':[11.5649985,11.3512804], 'w0':[5.6407612,7.342365 ],\ 'logM0': [4.9071932,3.1795786,],\ 'alpha':[10.6279446,3.7658774], 'logM1':[11.7621938,5.0188608], 'sigma_logM':[4.7031938, 4.6846614], 'logMmin':[1.0, 1.0], 'amp':[-12.0550382, 0.0,-1.5383083], 'r':[0.0, 0.0]} names = ['amp'] names.extend(emu.get_param_names()) from itertools import cycle names = cycle(names) amp_count = 0 v = [] for n in names: if n== 'amp': amp_count+=1 v.append(zhongzhu_dict[n][amp_count-1]) #this is a poison hack dont judge me #v.append(zhongzhu_dict[n][amp_count]) #this is a poison hack dont judge me if amp_count==3: break v = np.array(v) print v

In [9]:
zhongzhu_dict = {'omch2':[0.2661017,1.8339794 ], 'ombh2':[0.1054246,10.6161248], 'ln10As':[1.1295944,2.2441632],\
                 'H0':[0.3643993,13.8155106],\
                'ns':[0.2408568,10.6371797], 'Neff':[11.5649985,11.3512804], 'w0':[5.6407612,7.342365 ],\
                 'logM0': [4.9071932,3.1795786,],\
                 'alpha':[10.6279446,3.7658774], 'logM1':[11.7621938,5.0188608], 'sigma_logM':[4.7031938, 4.6846614], 'logMmin':[1.0, 1.0],
                'amp':[-12.0550382, 0.0], 'r':[0.0, 0.0]}

names = ['amp']
names.extend(emu.get_param_names())
from itertools import cycle
names = cycle(names)
amp_count = 0
v = [-1.5383083]
for n in names:
    if n== 'amp':
        amp_count+=1
    if amp_count==3:
        break
    if n in zhongzhu_dict:
        v.append(zhongzhu_dict[n][amp_count-1]) #this is a poison hack dont judge me
    else:
        v.append(1.0)
    #v.append(zhongzhu_dict[n][amp_count]) #this is a poison hack dont judge me


        
v = np.array(v)
v = np.array([-1.59585658e+02, -4.28609431e+00, 5.26421111e+00, 2.23600305e+00, 3.31933458e+00, 3.94369625e+00, 5.49058429e+00, 1.18077464e+02, 1.39672483e+00, 6.05653776e+00, 8.85934071e+01, 5.02868853e+00, 6.16913182e+00, 9.16003441e+02, -2.56178530e+00, 2.34222918e+00, -3.01172284e-01, 4.39990318e-01, 1.54266085e+00, 1.48328501e+02, 9.24908910e-01, 1.67154090e+00, 1.48260489e+00, 1.27269583e+00, -8.56570699e+00, -2.53310968e+00, -4.39899560e+00])
v = np.exp(np.array([ -12.05698765, -5.10156767, 4.99788515, 2.81202948, 5.07089351, 4.05994827, 5.32781005, 5.11743347, 4.3098708 , 1.47288301, 104.8856619 , 1.8027826 , 4.16014079, -1.07072829, 1.0, 32.70689385, 12.00788258, 97.2300853 , 42.13811969, 13.22048025, 100.17968175, 32.05458436, 8.88193458, 118.77917715, 8.56671981, 10.72995083, 1.0]) )

In [10]:
#emu._emulator.set_parameter_vector(v)
for _emulator in emu._emulators:
    _emulator.set_parameter_vector(v)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-742bdddd1835> in <module>()
      1 #emu._emulator.set_parameter_vector(v)
      2 for _emulator in emu._emulators:
----> 3     _emulator.set_parameter_vector(v)

/u/ki/swmclau2/.conda/envs/hodemulator/lib/python2.7/site-packages/george/modeling.pyc in set_parameter_vector(self, vector, include_frozen)
    248             v[:] = vector
    249         else:
--> 250             v[self.unfrozen_mask] = vector
    251         self.parameter_vector = v
    252         self.dirty = True

ValueError: NumPy boolean array indexing assignment cannot assign 27 input values to the 14 output values where the mask is true

In [ ]:
gof = emu.goodness_of_fit(training_file, N = 1000, statistic = 'log_frac')
print gof.mean()

In [ ]:
gof = emu.goodness_of_fit(test_file, statistic = 'log_frac')
print gof.mean(), np.median(gof)
plt.hist(np.log10(gof) );
from sklearn.model_selection import train_test_split
x, y, yerr = emu.x, emu.y, emu.yerr downsample_idxs = np.random.choice(x.shape[0], size = int(0.08*x.shape[0]), replace = False) x,y, yerr = x[downsample_idxs, :], y[downsample_idxs], yerr[downsample_idxs] train_x, test_x, train_y, test_y, train_yerr, test_yerr = train_test_split(x, y, yerr, test_size = 0.1)

In [ ]:
emu.x.shape

In [ ]:
n_cosmo_params = 7
loo_cosmo = emu.x[0, 0,  :n_cosmo_params]

loo_cosmo_idxs = np.all(emu.x[:, :,:n_cosmo_params] == loo_cosmo, axis =2)
train_x, train_y, train_yerr = emu.x[~loo_cosmo_idxs, :], emu.y[ ~loo_cosmo_idxs], emu.yerr[ ~loo_cosmo_idxs]
test_x, test_y, test_yerr = emu.x[loo_cosmo_idxs, :], emu.y[loo_cosmo_idxs], emu.yerr[loo_cosmo_idxs]
train_x, train_y, train_yerr = emu.x, emu.y, emu.yerr

In [ ]:
model = emu._emulator
model.compute(train_x, train_yerr)
test_x, test_y, test_yerr, _ = emu.get_data(test_file,fixed_params, None)

In [ ]:
pred_y = model.predict(train_y, test_x, False, False, False)*emu._y_std + emu._y_mean

In [ ]:
np.mean(np.abs((pred_y-test_y)/test_y))
#np.mean(np.abs((pred_y-train_y)/train_y))
for idx in xrange(50): plt.plot(emu.scale_bin_centers, ypred[idx*emu.n_bins:(idx+1)*emu.n_bins], label = 'Emu') plt.plot(emu.scale_bin_centers, emu.y[idx*emu.n_bins:(idx+1)*emu.n_bins], label = 'True') plt.title(np.sum(emu.x[(idx+1)*emu.n_bins, :-1]) ) plt.legend(loc='best') plt.xscale('log') plt.show()

In [ ]:
resids = np.abs(emu.y*emu._y_std+emu._y_mean - ypred)

In [ ]:
np.mean(resids/(emu.y*emu._y_std+emu._y_mean))

In [ ]:
ypred.mean(), emu._y_mean
plt.plot(emu.scale_bin_centers, np.abs(gof.mean(axis = 0)) ) plt.plot(emu.scale_bin_centers, np.ones_like(emu.scale_bin_centers)*0.01) plt.plot(emu.scale_bin_centers, np.ones_like(emu.scale_bin_centers)*0.05) plt.plot(emu.scale_bin_centers, np.ones_like(emu.scale_bin_centers)*0.1) plt.loglog();
plt.plot(emu.scale_bin_centers, np.abs(gof.T),alpha = 0.1, color = 'b') plt.plot(emu.scale_bin_centers, np.ones_like(emu.scale_bin_centers)*0.01, lw = 2, color = 'k') plt.loglog();

In [ ]:
test_gof = emu.goodness_of_fit(test_file, statistic = 'log_frac')
print test_gof.mean()

In [ ]:
test_gof = emu.goodness_of_fit(test_file, statistic = 'frac')
print test_gof.mean()

In [ ]:
plt.hist(np.log10(test_gof));

In [ ]:
test_x

In [ ]:
(emu.x*emu._x_std) + emu._x_mean

In [ ]:
emu.get_param_names()

In [ ]:
test_x_white, test_y_white = (test_x - emu._x_mean)/(emu._x_std + 1e-5), (test_y - emu._y_mean)/(emu._y_std + 1e-5)

In [ ]:
model = emu._emulator

In [ ]:
pred_y_white = model.predict(emu.y, test_x_white, False, False, False)

In [ ]:
pred_y = pred_y_white*emu._y_std + emu._y_mean

In [ ]:
plt.plot(pred_y[:100], label = 'pred')
plt.plot(test_y[:100], label = 'truth')

plt.legend(loc = 'best')

In [ ]:
test_y.mean(), emu._y_mean, pred_y.mean()

In [ ]:
test_y.std(), emu._y_std, pred_y.std()

In [ ]:
plt.hist(pred_y_white, bins = np.linspace(-3, 3, 100), label = 'Pred')
plt.hist(test_y_white, bins = np.linspace(-3, 3, 100), label = 'Test', alpha = 0.4);
plt.legend(loc = 'best')

In [ ]:


In [ ]: